package com.ba.base.utils

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import com.ba.base.R
import com.ba.base.api.ApiAuth
import com.google.gson.Gson
import com.ba.base.model.vo.UserDO
import com.ba.base.model.vo.UserEntity

object SharedPreUtils {
    private const val SHARED_PRE = "XC_SHARED_PRE"
    private const val USER_INFO = "USER_INFO"
    private const val HOUSE_INFO = "HOUSE_INFO"
    private const val User_DO = "User_DO"
    private const val BASE_URL = "m-cloud-base_url"

    /**
     * 清除share文件
     */
    fun clear(context: Context) {
        val   sharedPreferences =
            context.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE)
        sharedPreferences.edit().clear().commit()
    }
    /**保存用户数据*/
    fun saveUserInfo(context: Context, userInfo: UserEntity) {
        ApiAuth.user = null
        val sp = context.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE)
        sp.edit().putString(USER_INFO, Gson().toJson(userInfo)).apply()
    }

    /**清除用户数据*/
    fun clearUserInfo(context: Context) {
        val sp = context.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE)
        sp.edit().putString(USER_INFO, null).apply()
    }

    /**获取用户数据*/
    fun getUserInfo(context: Context): UserEntity? {
        val sp = context.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE)
        val user = sp.getString(USER_INFO, "")
        return if (TextUtils.isEmpty(user)) {
            null
        } else {
            val userInfo = user?.replace("\" -- \"", "null")
            Gson().fromJson(userInfo, UserEntity::class.java)
        }
    }

    /**保存主题*/
    fun saveTheme(context: Context?, @ColorInt value: Int) {
        context?.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE)?.edit()
            ?.putInt("m-cloud-theme", value)?.apply()
    }

    /**获取主题*/
    fun getTheme(context: Context): Int {
        val sp = context.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE)
        val defaultColor =  ContextCompat.getColor(context, R.color.colorPrimaryDark)
        val color = sp.getInt("m-cloud-theme", defaultColor)
        return if (color != 0 && Color.alpha(color) != 255) {
            defaultColor
        } else {
            color
        }
    }


    fun saveSearchHistory(context: Context?, name: String) {
        context ?: return
        val history = LinkedHashSet<String>()
        history.add(name)
        history.addAll(getSearchHistory(context))
        context.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE).edit()
            .putStringSet("m-cloud-search-history", history)?.apply()
    }

    fun clearSearchHistory(context: Context?) {
        context ?: return
        context.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE).edit()
            .remove("m-cloud-search-history")?.apply()
    }

    fun getSearchHistory(context: Context): Set<String> {
        val sp = context.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE)
        return sp.getStringSet("m-cloud-search-history", setOf())!!
    }



    /**保存UserDO数据*/
    fun saveUserDo(context: Context, userDO: UserDO) {
        ApiAuth.user = null
        val sp = context.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE)
        sp.edit().putString(User_DO, Gson().toJson(userDO)).apply()
    }

    /**清除UserDO数据*/
    fun clearUserDo(context: Context) {
        val sp = context.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE)
        sp.edit().putString(User_DO, null).apply()
    }

    /**获取UserDO数据*/
    fun getUserDo(context: Context): UserDO? {
        val sp = context.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE)
        val house = sp.getString(User_DO, "")
        return if (TextUtils.isEmpty(house)) {
            null
        } else {
            val userDO = house?.replace("\" -- \"", "null")
            Gson().fromJson(userDO, UserDO::class.java)
        }
    }
    fun saveIsLogin(context: Context?, isLogin: String) {//是否已登录 1 已登录 0为登录
        context?.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE)?.edit()
            ?.putString("isLogin", isLogin)?.apply()
    }

    fun getIsLogin(context: Context): String {
        val sp = context.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE)
        return sp.getString("isLogin", null) ?: "0"
    }
    fun saveToken(context: Context?, token: String) {//token
        context?.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE)?.edit()
            ?.putString("token", token)?.apply()
    }

    fun getToken(context: Context): String {
        val sp = context.getSharedPreferences(SHARED_PRE, Context.MODE_PRIVATE)
        return sp.getString("token", null) ?: ""
    }
}